import logging

from .base import BaseMac

logger = logging.getLogger('TdmaSimpleMac')

class TdmaSimpleMac(BaseMac):

    def __init__(self, env, node, phy):
        self.env = env
        self.phy = phy
        self.node = node

    def start(self):
        self.env.process(self.phy.start())
        self.env.process(self.timeslot_event())
        self.env.process(self.receive_event())

    def timeslot_event(self):
        while True:
            yield self.phy.wait_timeslot()
            f, s = self.phy.fs()
            if self.node.id == s:
                logger.debug(f'{self.env.now:.6f} {self.node.id} ({f},{s}) tx')
                self.env.process(self.phy.tx({'message':'helloworld', 'tag': 'TEST'}))

    def receive_event(self):
        while True:
            yield self.phy.wait_receive()